package cn.itcast.www.xmlTest;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.util.Collection;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ParseXml{
	 private static Document document;  
     
     public static void init() {  
         try {  
             DocumentBuilderFactory factory = DocumentBuilderFactory  
                     .newInstance();  
             DocumentBuilder builder = factory.newDocumentBuilder();  
             document = builder.newDocument();  
         } catch (ParserConfigurationException e) {  
             System.out.println(e.getMessage());  
         }  
     }  
    @Test
     public void createXml() {
    	String fileName = "D:/test.xml";
    	 DocumentBuilderFactory factory = DocumentBuilderFactory  
                 .newInstance();  
         DocumentBuilder builder = null;
		try {
			builder = factory.newDocumentBuilder();
		} catch (ParserConfigurationException e1) {
			e1.printStackTrace();
		}  
         document = builder.newDocument();
         Element root = document.createElement("Comment");//创建根节点  
         document.appendChild(root);  
           
        
         Element data= document.createElement("Data");  
         root.appendChild(data);  
         Element name= document.createElement("name");
         name.appendChild(document.createTextNode("张三"));
         Element content= document.createElement("content");
         content.appendChild(document.createTextNode("这个话题真是不错！"));
         Element date= document.createElement("date");
         DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
         date.appendChild(document.createTextNode(dateFormat.format(new Date())));
         data.appendChild(name);
         data.appendChild(content);
         data.appendChild(date);
           
         //将DOM对象document写入到xml文件中  
         TransformerFactory tf = TransformerFactory.newInstance();  
         try {  
             Transformer transformer = tf.newTransformer();  
             DOMSource source = new DOMSource(document);  
             transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");  
             transformer.setOutputProperty(OutputKeys.INDENT, "yes");  
             PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));  
             StreamResult result = new StreamResult(pw);  
             transformer.transform(source, result);     //关键转换  
             System.out.println("生成XML文件成功!");  
         } catch (TransformerConfigurationException e) {  
             System.out.println(e.getMessage());  
         } catch (IllegalArgumentException e) {  
             System.out.println(e.getMessage());  
         } catch (FileNotFoundException e) {  
             System.out.println(e.getMessage());  
         } catch (TransformerException e) {  
             System.out.println(e.getMessage());  
         }  
     }  
    
     public void parserXml(String fileName) {  
         try {  
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
             DocumentBuilder db = dbf.newDocumentBuilder();  
             Document document = db.parse(fileName);  
                
             NodeList employees = document.getChildNodes();  
             for (int i = 0; i < employees.getLength(); i++) {  
                 Node employee = employees.item(i);  
                 NodeList employeeInfo = employee.getChildNodes();  
                 for (int j = 0; j < employeeInfo.getLength(); j++) {  
                     Node node = employeeInfo.item(j);  
                     NodeList employeeMeta = node.getChildNodes();  
                     for (int k = 0; k < employeeMeta.getLength(); k++) {  
                         System.out.println(employeeMeta.item(k).getNodeName()  
                                 + ":" + employeeMeta.item(k).getTextContent());  
                     }  
                 }  
             }  
             System.out.println("解析完毕");  
         } catch (FileNotFoundException e) {  
             System.out.println(e.getMessage());  
         } catch (ParserConfigurationException e) {  
             System.out.println(e.getMessage());  
         } catch (SAXException e) {  
             System.out.println(e.getMessage());  
         } catch (IOException e) {  
             System.out.println(e.getMessage());  
         }  
     }  
     

}